package cn.edu.scau.cmi.chenmiaopeng.controller.compositePattern;
import javafx.stage.Stage;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.Label;

import java.util.ArrayList;

import cn.edu.scau.cmi.chenmiaopeng.compositePattern.safePattern.*;
import cn.edu.scau.cmi.chenmiaopeng.compositePattern.transparentPattern.*;

public class CreateOrganizationController {
	    static Stage stage;
	    static boolean  isSafe;
	    private static String nowSelectOrg = null;
	    @FXML
	    private Button confirm;

	    @FXML
	    private RadioButton createTeam;

	    @FXML
	    private  Button back;

	    @FXML
	    private RadioButton createPerson;
	    
	    @FXML
	    private TextField input;
	    
	    private static Label warning;
	   
	    private ArrayList<String>  addItemNames = new ArrayList<String>();  //用户增加organization名 
	    private static boolean createSuccess = false;  //controller要设成static,否则各controller对象不同可能导致值不同，一边为true,一边为false
	    public void setStage(Stage s) {
	    	stage = s;
	    }
	    public static void setSafeType(boolean safe) {
	    	isSafe = safe;
	    }
	    public void setLabelWarning(Label w) {  //设置提示信息
	    	warning = w;
	    }
	    public boolean isAddSuccess() {
	    	return createSuccess;
	    }
	    public String[] getAddItemNames() {  //主界面获得用户增加的organization名，若为null，则没增加成功，应不做任何改变
	    	return addItemNames.toArray(new String[addItemNames.size()]);
	    }
	    @FXML
	    public void clickConfirm(ActionEvent event) {
	    	if(isSafe) {
	    		Organization_S father;  //去找要添加节点的父节点
	    		InquiryOrganization inquiry = new InquiryOrganization();
	    		nowSelectOrg = CompositePatternController.getNowSelectOrg();
	    		System.out.println("当前获得的org为："+nowSelectOrg);
		    	   if(nowSelectOrg.equals("orginal organization")) {
		    		   father = CompositePatternController.getOriginalOrganization_S();
		    	   }
		    	   else {
		    		   father = inquiry.getOrganization_S(CompositePatternController.getOriginalOrganization_S(), nowSelectOrg);  //找父节点
		    	   }
//		    	   Team father_t = (Team)father;
		    	   if(createPerson.isSelected()) {
		    		   Team_S.setIsPerson(true);
		    		   
		    	   }
		    	   else {
		    		   Team_S.setIsPerson(false);
		    	   }
	        	  String mess;  //input.getText()才是当前输入的要创建的实体名
	        	  createSuccess = false;
		    	   if(father instanceof Team_S) {
		    		   Team_S father_t = (Team_S)father;
		    		   mess = father_t.add(input.getText());
		    	   }
		    	   else {
		    		   mess = "个体不能添加组织！";
		    	   }
	        	  if(Team_S.getIsCreateSuccess()) {
	        		  addItemNames.add(input.getText());
	        		  createSuccess = true;
	        	  }
	        	  warning.setText(mess);
	    	}
	    	else{
	    		Organization_T father;  //去找要添加节点的父节点
	    		InquiryOrganization inquiry = new InquiryOrganization();
	    		nowSelectOrg = CompositePatternController.getNowSelectOrg();
		    	   if(nowSelectOrg.equals("orginal organization")) {
		    		   father = CompositePatternController.getOriginalOrganization_T();  //用户没有选择父节点或者选择了根节点时
		    	   }
		    	   else {
		    		   System.out.println("现在选择的节点是："+nowSelectOrg);
		    		   father = inquiry.getOrganization_T(CompositePatternController.getOriginalOrganization_T(), nowSelectOrg);  //找父节点
		    	   }
//		    	   Team father_t = (Team)father;
		    	   if(createPerson.isSelected()) {
		    		   Team_T.setIsPerson(true);
		    		   
		    	   }
		    	   else {
		    		   Team_T.setIsPerson(false);
		    	   }
		        	  createSuccess = false;
		        	  String mess = father.add(input.getText());  //input.getText()才是当前输入的要创建的实体名
		          if(father instanceof Team_T) {
		        	  if(Team_T.getIsCreateSuccess()) {
		        		  addItemNames.add(input.getText());
		        		  createSuccess = true;
		        	  }
		          }
		          else {  //当前的是透明模式下的个人
		        	  createSuccess = false;
		          }
	        	  
	        	  warning.setText(mess);
	    	}
	    	
        }

	    @FXML
	    public void clickBack(ActionEvent event) {
         stage.close();
	    }

}
